﻿using System;
using System.Configuration;

namespace CoolCode.Web.Mvc.Configuretion
{
    /// <summary>
    /// 路由规则配置项集合
    /// </summary>
    public class RoutingCollection : ConfigurationElementCollection
    {
        public RoutingItem this[int index]
        {
            get
            {
                return base.BaseGet(index) as RoutingItem;
            }

            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }

                this.BaseAdd(index, value); 
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new RoutingItem();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((RoutingItem)element).Name;
        }

        [ConfigurationProperty("default", IsRequired = true)]
        public string Default {
            get { return Convert.ToString(this["default"]); }
        }

        [ConfigurationProperty("enable", IsRequired = true, DefaultValue = true)]
        public bool Enable {
            get { return Boolean.Parse(this["enable"].ToString()); }
        }

        public RoutingCollection()
        {
            this.AddElementName = "route";
        }
    }
}
